package 算法;

import java.util.Arrays;
import java.util.Random;

public class 排序 {
    public static void main(String[] args) {
        Random random = new Random();
        int[] a = random.ints(1000,0,1000).toArray();
        long startime = System.currentTimeMillis();
        maopao(a);
//        xuanze(a);
        long endtime = System.currentTimeMillis();
        System.out.println(Arrays.toString(a));
        System.out.println("程序运行的时间为： "+(endtime-startime)+"ms");
        }
        public static void maopao(int[] a){
            //        冒泡排序  相邻元素两两比较 大的往上冒
            for (int i = 0; i<a.length-1;i++){
                for (int j = 0;j<a.length-1-i;j++){
                    if (a[j] > a[j+1]){
                        int temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = temp;
                    }
                }
            }
        }
        public  static void xuanze(int[] a){
        //  选择排序
        for (int i=0;i<a.length-1;i++){
            int k=i;
            for (int j =k+1;j<a.length;j++){
                if (a[j] < a[k]) {
                    k=j;
                }
            }
            if (k!=i){
                int temp = a[k];
                a[k] = a[i];
                a[i] = temp;
            }

        }
    }

}

